package org.Niu;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamLearn {
    public static void main(String[] args) {
        // 1. 创建 Stream
        List<String> list = Arrays.asList("aaac", "bc", "cccc");
        Stream<String> stream = list.stream(); // 顺序流
        Stream<String> stringStream = list.parallelStream();  // 并行流

        String[] arr = {"a","b","c"};
        Stream<String> stream1 = Arrays.stream(arr);

        Stream<String> a = Stream.of("a", "bc", "ac");
        // 2. 常用中间操作
        List<String> a1 = a.filter(s -> s.startsWith("a")).collect(Collectors.toList());
        System.out.println("a1 = " + a1);

        //List<Integer> collect = list.stream().map(str -> str.length()).collect(Collectors.toList());
        List<Integer> collect = list.stream().map(String::length).collect(Collectors.toList());
        System.out.println("collect = " + collect);

        List<Integer> collect1 = Stream.of("100", "200", "300").map(str -> Integer.valueOf(str)).collect(Collectors.toList());
        System.out.println("collect1 = " + collect1);


        List<String> words = Arrays.asList("Hello", "World");
        List<String> letters = words.stream()
                .flatMap(word -> Arrays.stream(word.split("")))
                .collect(Collectors.toList());
        System.out.println("letters = " + letters);


        ArrayList<String> strings = new ArrayList<>(Arrays.asList("a", "b", "c"));
        strings.add("a");
        strings.addAll(Arrays.asList("a","b","c"));
        List<String> collect2 = strings.stream().distinct().collect(Collectors.toList());
        System.out.println("collect2 = " + collect2);


        List<String> list1 = List.of("100", "200", "3", "4", "5", "6" ,"6");
        List<Integer> collect3 = list1.stream().distinct().map(Integer::valueOf).sorted().limit(4).skip(2).toList();
        System.out.println("collect3 = " + collect3);

        list1.stream().distinct().map(Integer::valueOf).sorted().limit(4).skip(2).forEach(str -> System.out.print(str + " "));


    }
}
